package com.blank.javawebexam.controler;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author BLANK
 * @date 2020/11/4
 */
@WebServlet(urlPatterns = ("/HandlerServlet"),initParams = {@WebInitParam(name="username",value = "root"),@WebInitParam(name="password",value = "root")})
public class HandleServlet extends HttpServlet {

    String name =null;
    String password = null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        name=config.getInitParameter("username");
        password=config.getInitParameter("password");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String inputName=req.getParameter("username");
        String inputPassword=req.getParameter("password");
        if(inputName.equals(name)&&inputPassword.equals(password)) {
            try {
                HttpSession session =req.getSession();
                req.getRequestDispatcher("WEB-INF/views/index.jsp").forward(req, resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            req.getRequestDispatcher("LoginServlet").forward(req, resp);

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
